#define _CRT_SECURE_NO_WARNINGS	1
class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        int sum = 0, n = nums.size();
        for (int i = 0; i < n; i++)
        {
            sum += nums[i];
        }
        int target = sum - x, window = 0;
        if (target < 0)
        {
            return -1;
        }
        if (nums[0] > x && nums[n - 1] > x) return -1;
        int len = 0, left = 0, right = 0;
        while (right < n)
        {
            window += nums[right];
            while (window > target) window -= nums[left++];
            if (window == target)
            {
                len = max(len, right - left + 1);
            }
            right++;
        }
        return n - len;

    }
};